home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-11-06 | 8.6 KB | 302 lines | [TEXT/KAHL] |
- // ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
- // • Program: FrameAnim
- // • File: Files.c
- // •
- // • Copyright © 1993 by Scott B. Steinman, O.D., Ph.D. All Rights Reserved.
- // ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
- // • NOTES:
- // • Animation files are saved in a PICS file format that includes:
- // • (a) One 'INFO' resource for playback information.
- // • (b) One 'PICT' (PICT2) resource for each stored image.
- // ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
-
- #include "FrameAnim.h"
-
- #include <Files.h>
- #include <StandardFile.h>
- #include <PictUtil.h>
-
- // • ------------------ External Globals ----------------------------------
-
- extern GWorldPtr gFrames[]; // • From Main.c file
- extern PaletteHandle gPalette; // • From Main.c file
- extern CTabHandle gCTable; // • From Main.c file
- extern Handle gMenuBar; // • From Main.c file
- extern Settings gSettings; // • From Main.c file
- extern Flags gFlags; // • From Main.c file
- extern Rect gBounds; // • From Animation.c file
-
- // • ------------------ Internal Globals ----------------------------------
-
- PicHandle gFramePICTs[ kMaxFrames ]; // • Frame Pictures
-
- // •------------------- Static Variables ----------------------------------
-
- static PICSInfoHandle sPicInfo; // • Animation parameters
-
- // • ------------------ Static Functions ----------------------------------
-
- static void DrawPICTToGWorld( const short );
- static void NewColorsFromPICT( void );
- static void ReadFilm( const StandardFileReply * );
- static void WriteFilm( const StandardFileReply * );
-
- // • ------------------ Save Frame Images in File -------------------------
-
- void
- SaveFilm( void )
- // •
- // • Save an animation sequence to animation file on disk.
- {
- StandardFileReply theReply;
- Str255 thePrompt, theDefaultName;
-
- strcpy( (char *) thePrompt, (char *) "\pSave Animation File as..." );
- strcpy( (char *) theDefaultName, (char *) "\p" );
-
- StandardPutFile( &thePrompt, &theDefaultName, &theReply );
-
- if (theReply.sfGood) {
-
- FSpCreateResFile( &theReply.sfFile, 'FRMA', 'PICS', theReply.sfScript );
- WriteFilm( &theReply );
- }
- }
-
- // • ------------------ Write the Frames to File --------------------------
-
- static void
- WriteFilm( const StandardFileReply *theReply )
- // •
- // • Writes entire animation sequence to animation file on disk.
- {
- OSErr resultCode;
- short resFile, i;
- Str255 frameText;
-
- // • Transfer animation information to PicsInfo record
-
- sPicInfo = (PICSInfoHandle) NewHandle( sizeof( PICSInfoRec ) );
- if (sPicInfo == NullHandle)
- ErrorHandler( kNoMemoryMsg, (char *) NilString, (char *) NilString,
- (char *) NilString );
-
- HLock( (Handle) sPicInfo );
- (**sPicInfo).speed = RoundUp( kRefreshRate / gSettings.frameDelay );
- (**sPicInfo).depth = kDepth;
- (**sPicInfo).version = 0;
- (**sPicInfo).creator = 'FRMA';
- (**sPicInfo).largest = 0; // • Not used.
- (**sPicInfo).bwColor = 1;
- HUnlock( (Handle) sPicInfo );
-
- // • Write the animation info and frames to resource fork of file
-
- resFile = FSpOpenResFile( &theReply->sfFile, fsWrPerm );
- AddResource( sPicInfo, 'INFO', kPICSInfoID, "\pAnimation Parameters" );
-
- for (i = 0; i < gSettings.numFrames; i++) {
- PtoCstr( (char *) frameText );
- sprintf( (char *) frameText, "Animation Frame %d of %ld", i + 1,
- gSettings.numFrames );
- CtoPstr( (char *) frameText );
-
- AddResource( gFramePICTs[ i ], 'PICT', kFirstPictID + i, frameText );
- UpdateResFile( resFile ); // • Force write of resource to file.
- }
-
- CloseResFile( resFile );
-
- // • Null handles to resources that have been freed by CloseResFile
-
- sPicInfo = NullHandle;
- for (i = 0; i < gSettings.numFrames; i++)
- gFramePICTs[ i ] = NullHandle;
-
- DisableItem( GetMHandle( kFileID ), kSaveFilmItem );
- }
-
- // • ------------------ Open Animation Sequence File ----------------------
-
- void
- OpenFilm( void )
- // •
- // • Retrieve entire animation sequence from animation file on disk.
- {
- SFTypeList typeList;
- StandardFileReply theReply;
- short numTypes;
-
- numTypes = 1; // • Only one type of file
- typeList[ 0 ] = 'PICS'; // • Only Animation files
-
- StandardGetFile( NullPointer, numTypes, &typeList, &theReply );
-
- if (theReply.sfGood)
- ReadFilm( &theReply );
- else
- gFlags.cancel = true;
- }
-
- // • ------------------ Read the Frames from File -------------------------
-
- static void
- ReadFilm( const StandardFileReply *theReply )
- // •
- // • This does the opposite of WriteFilm.
- {
- PictInfo pictInfo;
- OSErr err;
- short resFile, i;
-
- // • Remove any previously-allocated color records
-
- if (gCTable != NullHandle) {
- DisposCTable( gCTable );
- gCTable = NullHandle;
- }
- if (gPalette != NullHandle) {
- DisposePalette( gPalette );
- gPalette = NullHandle;
- }
-
- // • Read the animation information resource.
-
- resFile = FSpOpenResFile( &theReply->sfFile, fsRdPerm );
-
- sPicInfo = (PICSInfoHandle) Get1Resource( 'INFO', kPICSInfoID );
-
- // • Pull out frame delay from PICS resource (other fields ignored here)
-
- HLock( (Handle) sPicInfo );
- gSettings.frameDelay = RoundUp( kRefreshRate / (**sPicInfo).speed );
- HUnlock( (Handle) sPicInfo );
-
- // • Prepare new frame GWorlds.
-
- PrepareFilm(); // • Allocate frame storage & update palettes.
-
- // • Find the number of frame image PICT resources.
-
- gSettings.numFrames = Count1Resources( 'PICT' );
-
- // • Copy contents of first PICT resource to first PICT record.
-
- DisposeFramePICTs();
- gFramePICTs[ 0 ] = GetPicture( kFirstPictID );
- DetachResource( gFramePICTs[ 0 ] );
-
- // • Get frame size info from first PICT.
-
- err = GetPictInfo( gFramePICTs[ 0 ], &pictInfo,
- returnColorTable+suppressBlackAndWhite, kNumColors, systemMethod, 0 );
- gSettings.frameSize = pictInfo.sourceRect.right - pictInfo.sourceRect.left;
-
- // • Get color info from PICT to reset color table and palette.
-
- HLock( (Handle) pictInfo.theColorTable );
- gSettings.bkgndGray = (**pictInfo.theColorTable).ctTable[ 0 ].rgb.red / 256;
- gSettings.targetGray = (**pictInfo.theColorTable).ctTable[ 1 ].rgb.red / 256;
- HUnlock( (Handle) pictInfo.theColorTable );
- DisposCTable( pictInfo.theColorTable );
-
- NewColorsFromPICT(); // • Create and initialize color table and palette.
-
- UpdateWindowColors();
- ResetForeAndBackColors();
- ResetWindBkgnd();
-
- DrawPICTToGWorld( 0 ); // • Transfer the frame image from PICT to GWorld.
-
- for (i = 1; i < gSettings.numFrames; i++) {
- gFramePICTs[ i ] = GetPicture( kFirstPictID + i );
- DetachResource( gFramePICTs[ i ] );
- DrawPICTToGWorld( i );
- }
- ResetForeAndBackColors();
-
- CloseResFile( resFile );
-
- // • Null out resource handles freed by CloseResFile
-
- sPicInfo = NullHandle;
- for (i = 0; i < gSettings.numFrames; i++)
- gFramePICTs[ i ] = NullHandle;
-
- SysBeep( 1 );
- }
-
- // • ------------------ Create and Initialize New CTable From PICT --------
-
- static void
- NewColorsFromPICT( void)
- // •
- // • Create and initialize color table and palette
- // • using background and target colors found in animation file.
- {
- RGBColor c;
- short i;
-
- // • Create new CTable and palette.
-
- gCTable = GetCTable( kGrayCTableID );
- DetachResource( gCTable );
- (*gCTable)->ctFlags |= 0x4000; // • Speeds up later CopyBits calls
- gPalette = NewPalette( kNumColors, gCTable, pmTolerant+pmExplicit, 0 );
-
- // • Initialize palette entries.
-
- c.red = c.green = c.blue = 0;
- for (i = 0; i < kNumColors; i++) {
- SetEntryColor( gPalette, i, &c );
- SetEntryUsage( gPalette, i, pmTolerant+pmExplicit, 0 );
- }
- c.red = c.green = c.blue = 65535;
- SetEntryColor( gPalette, kPalWhite, &c );
-
- // • Force palette color entries into CTable.
-
- Palette2CTab( gPalette, gCTable );
- }
-
- // • ------------------ Draw Contents of Frame PICT into GWorld -----------
-
- static void
- DrawPICTToGWorld( const short frm )
- // •
- // • Draw the contents of an animation file into the GWorlds.
- {
- GDHandle currentDev = NullHandle;
- CGrafPtr currentPort = NullPointer;
-
- GetGWorld( ¤tPort, ¤tDev ); // • Save current port & device
-
- if (gFramePICTs[ frm ] != NullHandle) {
- HLock( (Handle) gFramePICTs[ frm ] );
- LockPixels( GetGWorldPixMap( gFrames[ frm ] ) );
- SetGWorld( gFrames[ frm ], NullPointer );
- DrawPicture( gFramePICTs[ frm ], &gBounds );
- SetGWorld( currentPort, currentDev ); // • Reset port & device
- UnlockPixels( GetGWorldPixMap( gFrames[ frm ] ) );
- HUnlock( (Handle) gFramePICTs[ frm ] );
- }
- }
-
- // • ------------------ Dispose of the Frame PICTs ------------------------
-
- void
- DisposeFramePICTs( void )
- // •
- // • Dispose of the animation Picture records.
- {
- short i;
-
- for (i = 0; i < kMaxFrames; i++) {
- if (gFramePICTs[ i ] != NullHandle) {
- KillPicture( gFramePICTs[ i ] );
- gFramePICTs[ i ] = NullHandle;
- }
- }
- }
-
-